package com.testing.month_exam_a;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.google.gson.Gson;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.squareup.okhttp.Call;
import com.squareup.okhttp.Callback;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;

import java.io.IOException;
import java.util.List;

public class MainActivity extends AppCompatActivity implements CallBack_2 {

    private RecyclerView left;
    private RecyclerView right;
    private RecyclerViewAdapter adapter;
    private RecyclerViewAdapter2 adapter2;
    private int index;
    private List<Bean.RsBean> rs;
    public static DisplayImageOptions op;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //
        op = new DisplayImageOptions.Builder()
                .cacheInMemory(true)
                .cacheOnDisk(true)
                .bitmapConfig(Bitmap.Config.RGB_565)
                .build();


        initView();

        getData();

        left.setLayoutManager(new LinearLayoutManager(this));//设置布局管理器

        right.setLayoutManager(new LinearLayoutManager(this));//设置布局管理器

    }

    @Override
    public void myIndex(int x) {
        Bean.RsBean rsBean = rs.get(x);
        List<Bean.RsBean.ChildrenBeanX> children = rsBean.children;

        adapter2 = new RecyclerViewAdapter2(MainActivity.this, children);
        right.setAdapter(adapter2);
    }


    private void initView() {

        left = (RecyclerView) findViewById(R.id.left);
        right = (RecyclerView) findViewById(R.id.right);
    }

    public void getData() {
        OkHttpClient okc = new OkHttpClient();
        Request request = new Request.Builder()
                .url("http://mock.eoapi.cn/success/4q69ckcRaBdxhdHySqp2Mnxdju5Z8Yr4")
                .build();

        Call call = okc.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Request request, IOException e) {

            }

            @Override
            public void onResponse(Response response) throws IOException {
                final String json = response.body().string();
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {

                        Gson g = new Gson();

                        Bean b = g.fromJson(json, Bean.class);

                        rs = b.rs;


                        adapter = new RecyclerViewAdapter(MainActivity.this, rs);
                        left.setAdapter(adapter);
                        adapter.okokok(MainActivity.this);

                        Bean.RsBean rsBean = rs.get(index);
                        List<Bean.RsBean.ChildrenBeanX> children = rsBean.children;

                        adapter2 = new RecyclerViewAdapter2(MainActivity.this, children);
                        right.setAdapter(adapter2);

                    }
                });
            }
        });
    }
}
